package com.trace.app.controller;

import com.google.common.collect.Maps;
import com.trace.app.entity.DTO.RecordDTO;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.StringUtils;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeoutException;

@Api(tags = "Record智能合约访问")
@RestController
@RequestMapping("/record/contract")
@Slf4j
@AllArgsConstructor
public class RecordContractController {

    final Gateway gateway;


    @GetMapping("/init")
    public Map<String, Object> init() throws ContractException {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] record = contract.evaluateTransaction("initLedger");

        result.put("payload", StringUtils.newStringUtf8(record));
        result.put("status", "ok");

        return result;
    }

    /**
     * 接口走的这个方法
     */
    @GetMapping("/{key}")
    public Map<String, Object> queryRecordByKey(@PathVariable String key) throws ContractException {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] record = contract.evaluateTransaction("queryRecord", key);

        result.put("payload", StringUtils.newStringUtf8(record));
        result.put("status", "ok");

        return result;
    }

    @PutMapping
    public Map<String, Object> createRecord(@RequestBody RecordDTO record) throws ContractException, TimeoutException, InterruptedException {
        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] bytes = contract.submitTransaction("createRecord", record.getKey(), String.valueOf(record.getProId()), String.valueOf(record.getUserId()),
                record.getLocation(), record.getRemarks(), record.getImg());

        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");

        return result;
    }

    @PostMapping
    public Map<String, Object> updateRecord(@RequestBody RecordDTO record) throws ContractException, TimeoutException, InterruptedException {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] bytes = contract.submitTransaction("updateRecord", record.getKey(), String.valueOf(record.getProId()),
                String.valueOf(record.getUserId()), record.getLocation(), record.getImg(), record.getRemarks());

        result.put("payload", StringUtils.newStringUtf8(bytes));
        result.put("status", "ok");

        return result;
    }

    @DeleteMapping("/{key}")
    public Map<String, Object> deleteRecordByKey(@PathVariable String key) throws ContractException, TimeoutException, InterruptedException {

        Map<String, Object> result = Maps.newConcurrentMap();
        Contract contract = getContract();
        byte[] cat = contract.submitTransaction("deleteRecord", key);

        result.put("payload", StringUtils.newStringUtf8(cat));
        result.put("status", "ok");

        return result;
    }

    private Contract getContract() {
        Network network = gateway.getNetwork("mychannel");
        // 获取合约
        return network.getContract("trace-contract");
//       return network.getContract("hyperledger-fabric-contract-java-demo");
    }
}
